<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>百商汇</title>
	<meta name="viewport" content="initial-scale = 1.0,maximum-scale = 1.0,minimum-scale = 1.0,user-scalable = no,width = device-width">
	<meta name="format-detection" content="telephone=no" />
	<link rel="stylesheet" type="text/css" href="../csses/buyMerCurrency.css">
</head>
<body>
	<div class="buyMerCurrency_title">
		购买商币
		<img src="../image/back.png" onclick="goBack()">
	</div>
	<div class="buyMerCurrency_box">
		<ul>
			<li class="cashNum">
				<span>现金金额(元)</span>
				<input type="text" placeholder="请输入金额" class="input"></input>
			</li>
			<li class="payPsw">
				<span>支付密码</span>
				<input type="password" placeholder="请输入百商汇支付密码" class="input"></input>
				<div class="choose">
					<img src="../image/psw.png">
				</div>
			</li>
			<li class="username">
				<span>商户账号</span>
				<input type="text" readonly="true" value=""></input>
			</li>
			<li class="selfCloudMoney">
				<span>商币数量(个)</span>
				<input type="text" readonly="true" value=""></input>
			</li>
			<li class="otherMobile">
				<span>会员账号</span>
				<input type="text" placeholder="请输入会员手机号" class="input"></input>
			</li>
			<li class="otherCloudMoney">
				<span>商币数量(个)</span>
				<input type="text" readonly="true" value=""></input>
			</li>
		</ul>
		<div class="explain">购买说明：15元可购买115个商币，其中15个商币给商家，100个商币给会员</div>
		<button class="submitBtn">确认</button>
		<div class="remindBox"></div>
	</div>

	<script type="text/javascript" src="../lib/flexible.js"></script>
	<script type="text/javascript" src="../lib/jquery-1.10.1.min.js"></script>
	<script type="text/javascript">
		onload = function(){
			$('.username input').val(sessionStorage.buyMobile);
		}

		$('.input').focus(function(){
			this.select();
		});

		function goBack(){
			window.history.back();
		}

		function remind(message){
			$('.remindBox').css('display','block').text(message);
			setTimeout(function(){
				$('.remindBox').css('display','none');
			},1500)
		}

		$('.cashNum input').blur(function(){
			var $cashNum = $('.cashNum input').val();
			if($cashNum.length > 0){
				var $totalNum = Math.round($cashNum/15*115*100)/100;
				var $selfNum = $cashNum;
				var $otherNum = $totalNum - $selfNum;
				$('.selfCloudMoney input').val($selfNum);
				$('.otherCloudMoney input').val($otherNum);
			}
		});

		$('.submitBtn').click(function(){
			var $payPassword = $('.payPsw input').val();
			var $cashNum = $('.cashNum input').val();
			var $selfCloudMoney = $('.selfCloudMoney input').val();
			var $otherCloudMoney = $('.otherCloudMoney input').val();
			var $otherMobile = $('.otherMobile input').val();
			var $remindBox = $('.remindBox');

			if(!(/^1[3|4|5|7|8]\d{9}$/.test($otherMobile)) || !$otherMobile){
				remind('请输入会员手机号');
			}else if(!$payPassword){
				remind('请输入密码');
			}else if(!$cashNum){
				remind('请输入充值金额');
			}else{
				$.ajax({
					type : 'POST',
					url : "/api/v1/cloud/buyCloudMoney",
					data : {
						paramStr: "{'userName':'" + sessionStorage.userName + "','token':'" + sessionStorage.token + "','money':'" + $cashNum + "','selfCloudMoney':'" + $selfCloudMoney + "','payPassword':'" + $payPassword + "','otherCloudMoney':'" + $otherCloudMoney + "','otherMobile':'" + $otherMobile + "'}"
					},
					dataType : "json",
					timeout : 10000,
					success : function (data) {
						remind(data.message);
						if(data.code == 200){
							$('.submitBtn').attr('disabled',true).css('background','#ccc');
							setTimeout(function(){
								window.history.back();
							},1500);
						}
					},
					error : function (xhr,type,errorThrown){
						// alert('未知错误');
					}
				});
			}
		});
	</script>
</body>
</html>